package com.kecheng.moban;

import com.kecheng.dao.PeizhiDao;
import net.sf.json.JSONObject;
import com.kecheng.pojo.Peizhi;
import com.kecheng.util.SpringUtil;

/**
 * 微信推送信息
 * 
 * @author My
 * @CreateDate 2016-1-19
 * @param
 */
public class WeixinPushMassage {
	private static PeizhiDao peizhiDao = (PeizhiDao) SpringUtil
			.getBean("peizhiDao");
	private static Peizhi peizhi = peizhiDao.find();

	// 下级关注通知
	public void guanzhu(String id, String time, String openid) {
		String appId = peizhi.getAppid();
		String appSecret = peizhi.getAppsecret();
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
		Token token = headtoken(appId, appSecret);// 获取token
		url = url.replace("ACCESS_TOKEN", token.getAccessToken());// 转换为响应接口模式

		// 封装数据
		JSONObject json = new JSONObject();
		json.put("touser", openid);// 接收者wxName
		json.put("template_id", "5C347vTlE258AqVMMB7jRccgUGDz4tGCiPG7gYVxGfk");// 消息模板
		// json.put("url", "http://weix");//填写url可查看详情

		JSONObject dd = new JSONObject();

		JSONObject dd2 = new JSONObject();
		dd2.put("value", "您好，您有下级成员扫描您的二维码进行关注哦。");// 消息提示
		dd2.put("color", "#173177");
		dd.put("first", dd2);

		JSONObject cc2 = new JSONObject();
		cc2.put("value", id);// 订单号
		cc2.put("color", "#173177");
		dd.put("keyword1", cc2);

		JSONObject ee2 = new JSONObject();
		ee2.put("value", time);// 货物类型
		ee2.put("color", "#173177");
		dd.put("keyword2", ee2);

		JSONObject gg2 = new JSONObject();
		gg2.put("value", "祝您生活愉快！");
		// gg2.put("color", "#173177");
		dd.put("remark", gg2);

		json.put("data", dd);
		System.out.println(json.toString());
		JSONObject js = CommonUtil.httpsRequest(url, "POST", json.toString());
		System.out.println("js==" + js);
	}

	/**
	 * 请求token
	 * 
	 * @Description :
	 * @param
	 * @return ---------------
	 * @Author : My
	 * @CreateData : 2016-1-18
	 */
	public static Token headtoken(String appId, String appSrecet) {
		Token token = new Token();
		token = CommonUtil.getToken(appId, appSrecet);
		return token;
	}
}
